﻿using System.Collections.Generic;
using Eneta.Portal.Common.Dto;
using Microsoft.SharePoint;

namespace Eneta.Portal.Common.Repository
{
    public static class ClassificatorsRepository
    {
        public static IList<KeywordDto> GetAllKeywords()
        { 
            var siteId = SPContext.Current.Site.ID;
            var keywords = new List<KeywordDto>();

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (var site = new SPSite(siteId))
                using (var web = site.OpenWeb())
                {
                    if (!web.HasList("Keywords"))
                        return;

                    foreach (SPListItem item in web.Lists["Keywords"].Items)
                    {
                        var keyword = new KeywordDto();
                        keyword.Id = item.ID;
                        keyword.Title = item.Title;
                        if (item["KeywordGroup"] != null)
                            keyword.GroupName = item["KeywordGroup"].ToString();
                        keywords.Add(keyword);
                    }
                }
            });

            return keywords;
        }
    }
}
